package com.ruoyi.web.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.domain.Area;
import com.ruoyi.web.mapper.AreaMapper;
import com.ruoyi.web.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private RestTemplate restTemplate;

    private final String URL = "https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=%s_full";

    @Override
    public List<Area> findListPage(Page page, Area area){
        return areaMapper.findListPage(page, area);
    }

    @Override
    public JSONObject getGeoJson(String areaCode) {
        String url = String.format(URL, areaCode);
        String stringObject = restTemplate.getForObject(url, String.class);
        JSONObject jsonObject = JSONObject.parseObject(stringObject);
        return jsonObject;
    }
}
